//
// Created by ChenJi on 2021/7/30.
//

#include "UartController.h"

uint8_t uart1_rx_buffer[256];
uint8_t uart3_rx_buffer[256];
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart3;


void HC_UartController_Init() {
    HAL_UARTEx_ReceiveToIdle_DMA(&huart1, uart1_rx_buffer, 256);
    HAL_UARTEx_ReceiveToIdle_DMA(&huart3, uart3_rx_buffer, 256);
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
    if (huart == &huart1){
        onUart1Message(uart1_rx_buffer, Size);
        HAL_UARTEx_ReceiveToIdle_DMA(&huart1, uart1_rx_buffer, 256);
    }
    if (huart == &huart3){
        onUart3Message(uart3_rx_buffer, Size);
        HAL_UARTEx_ReceiveToIdle_DMA(&huart3, uart3_rx_buffer, 256);
    }


}
void sendMessageUart1(uint8_t*data,uint8_t size){
    HAL_UART_Transmit(&huart1,data,size,0xFFFF);
}
void sendMessageUart3(uint8_t*data,uint8_t size){
    HAL_UART_Transmit(&huart3,data,size,0xFFFF);
}
void sendMessageUart1_DMA(uint8_t*data,uint8_t size){
    HAL_UART_Transmit_DMA(&huart1,data,size);
}
void sendMessageUart3_DMA(uint8_t*data,uint8_t size){
    HAL_UART_Transmit_DMA(&huart3,data,size);
}